package io.unicorn.modules.watersub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;
import io.unicorn.modules.sys.entity.SysDeptEntity;
import io.unicorn.modules.sys.entity.SysUserEntity;
import io.unicorn.modules.sys.service.SysDeptService;
import io.unicorn.modules.sys.shiro.ShiroUtils;
import io.unicorn.modules.water.dao.VaccountmanageDao;
import io.unicorn.modules.water.entity.ProDataAnalyseEntity;
import io.unicorn.modules.water.entity.VaccountmanageEntity;
import io.unicorn.modules.watersub.service.VaccountmanagesubService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("vaccountmanagesubService")
public class VaccountmanagesubServiceImpl extends ServiceImpl<VaccountmanageDao, VaccountmanageEntity> implements VaccountmanagesubService {
    @Autowired
    SysDeptService sysDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key   = (String)params.get("key");
        String value = (String)params.get("value");

        SysUserEntity user = ShiroUtils.getUserEntity();
        SysDeptEntity dept = sysDeptService.getById(user.getDeptId());
        Long deptId =  Long.parseLong(dept.getOrderNum().toString());

        Map<String , Object> queryMap = new HashMap<>();
        queryMap.put("unit_name_w_id", deptId);

        IPage<VaccountmanageEntity> page = this.page(
                new Query<VaccountmanageEntity>().getPage(params),
                new QueryWrapper<VaccountmanageEntity>()
                        .allEq(deptId > 0, queryMap, false)
                        .like(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(key), key, value)
                        .apply("pro_name not  in (select 小区名称 from [Gather_DB].[dbo].[v_stop_view_data])")
        );

        return new PageUtils(page);
    }


    @Override
    public List<Map<String, Object>> execGetChartDataSub(Map<String, Object> params) {
        return baseMapper.execGetChartDataSub(params);
    }

    @Override
    public PageUtils execGetAlertSub(Map<String, Object> params) {

        Integer pageSize =10;
        if (params.get("limit")!=null) pageSize =(Integer.parseInt(params.get("limit").toString()));
        Integer currPage = 1;
        if (params.get("page")!=null) currPage = (Integer.parseInt(params.get("page").toString()));

        List<VaccountmanageEntity> dataList = baseMapper.execGetAlertSub(params);
        Integer total = dataList.size();
        //从第几条数据开始
        Integer firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        Integer lastIndex = currPage * pageSize;

        lastIndex = lastIndex > total ? total : lastIndex;

        //System.out.println(dataList);

        return new PageUtils(dataList.subList(firstIndex, lastIndex),total,pageSize,currPage);
    }
}
